package com.gzy.rpsm_admin.interceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从HTTP请求头Header中获取携带的token信息
        String token = request.getHeader("Authorization");
        // 如果token信息存在，则放行
        // 之后会实际校验token是不是我们之前在用户登录时返回给他的token
        if(Optional.ofNullable(token).isPresent()){
            return true;
        }else{//否则，返回403错误
            response.sendError(403,"Authorization无信息，未登录");
            return false;
        }
    }
}
